package com.h1909d.weinfo.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.h1909d.weinfo.R;
import com.h1909d.weinfo.bean.SectionsBean;

import java.util.ArrayList;

public class RlvSectionAdapter extends RecyclerView.Adapter<RlvSectionAdapter.ViewHolder> {

    private Context context;
    private ArrayList<SectionsBean.DataBean> dataBeans;

    public RlvSectionAdapter(Context context, ArrayList<SectionsBean.DataBean> dataBeans) {
        this.context = context;
        this.dataBeans = dataBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_section, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(dataBeans.get(position).getThumbnail()).into(holder.img);
        holder.tv.setText(dataBeans.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    public void setData(SectionsBean sectionsBean) {
        dataBeans.addAll(sectionsBean.getData());
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView img;
        TextView tv;

        public ViewHolder(View itemView) {
            super(itemView);

            img = itemView.findViewById(R.id.img);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}
